package com.my.goodsmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.my.goodsmanage.entity.Password;
import com.my.goodsmanage.entity.R;
import com.my.goodsmanage.entity.Result;
import com.my.goodsmanage.entity.User;
import com.my.goodsmanage.entity.form.LayuiTableVo;
import com.my.goodsmanage.service.PasswordService;
import com.my.goodsmanage.utils.AESUtil;
import com.my.goodsmanage.utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.ws.Action;
import java.util.Date;
import java.util.List;

/**
 * @author 徐佳辉
 * @date 2020/12/13 15:22
 */
@Controller
@RequestMapping("/pass")
@Slf4j
public class PasswordController {


    @Autowired
    private PasswordService passwordService;


    /**
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public LayuiTableVo list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) throws Exception {
        PageHelper.startPage(page, limit);

        QueryWrapper<Password> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        PageInfo<Password> pageInfo = new PageInfo<>(passwordService.list(wrapper));
        List<Password> list = pageInfo.getList();
        //解密
        for (Password password : list) {
            password.setPassword(AESUtil.decrypt(password.getPassword(), Md5Utils.string2Md5_16(String.valueOf(password.getUserId()))));
        }


        return LayuiTableVo.ok(list, (int) pageInfo.getTotal());
    }


    /**
     * 插入
     *
     * @param password
     * @param session
     * @return
     */
    @PostMapping("insert")
    @ResponseBody
    public R insert(Password password, HttpSession session) throws Exception {
        log.info("password:{}", password);
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return R.error("请登录");
        }


        if (password.getId() != null) {
            password.setPassword(AESUtil.encrypt(password.getPassword(), Md5Utils.string2Md5_16(String.valueOf(password.getUserId()))));
            password.setUpdateTime(new Date());

            QueryWrapper<Password> queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", password.getId());
            boolean update = passwordService.update(password, queryWrapper);
            return update ? R.success("更新成功") : R.error("更新失败");
        } else {
            password.setUserId(user.getUserId());
            //aes加密，密钥为用户id MD516位
            password.setPassword(AESUtil.encrypt(password.getPassword(), Md5Utils.string2Md5_16(String.valueOf(user.getUserId()))));
            password.setCreateTime(new Date());

            boolean save = passwordService.save(password);

            return save ? R.success("添加成功") : R.error("添加失败");
        }

    }


    /**
     * 删除根据主键
     *
     * @param id
     * @return
     */
    @PostMapping("deleteOne")
    @ResponseBody
    public R del(Integer id) {
        boolean removeById = passwordService.removeById(id);
        return removeById ? R.success("删除成功") : R.error("删除失败");
    }


    /**
     * 更新
     *
     * @param password 要有主键 和用户id
     * @return
     * @throws Exception
     */
    @PostMapping("update")
    @ResponseBody
    public R update(Password password) throws Exception {
        password.setPassword(AESUtil.encrypt(password.getPassword(), Md5Utils.string2Md5_16(String.valueOf(password.getUserId()))));
        password.setUpdateTime(new Date());

        QueryWrapper<Password> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", password.getId());
        boolean update = passwordService.update(password, queryWrapper);
        return update ? R.success("更新成功") : R.error("更新失败");
    }


    @PostMapping("get")
    @ResponseBody
    public R get(Integer id) {
        Password byId = passwordService.getById(id);
        return R.success(byId);
    }

}
